package com.cykj.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @BelongsProject: jd-project
 * @BelongsPackage: com.cykj.config
 * @Author: 小林
 * @CreateTime: 2025-02-24 20:44
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        //1、创建模板
        RedisTemplate<String, Object> redisTemp = new RedisTemplate<String,Object>();
        //2、设置连接
        redisTemp.setConnectionFactory(factory);
        //3、给key序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemp.setKeySerializer(stringRedisSerializer);
        redisTemp.setHashKeySerializer(stringRedisSerializer);
        //4、给value序列化
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemp.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemp.setHashValueSerializer(genericJackson2JsonRedisSerializer);


        return redisTemp;
    }
}

